#pragma once

#include "./earcut.hpp"
#include "qlib/core/error.h"
#include "qlib/geo/polygon.h"

namespace qlib::tools {
static std::vector<uint32_t> tri_earcut(const Vec2dArray& points) {
  // Create array
  std::vector<std::vector<std::array<double, 2>>> polygon;
  // Create polyline
  std::vector<std::array<double, 2>> polyline;
  for (auto& p : points) {
    polyline.push_back({p.x, p.y});
  }
  polygon.push_back(polyline);

  auto indices = mapbox::earcut<uint32_t>(polygon);
  return indices;
}

static std::vector<uint32_t> tri_earcut(Polygon::s_ptr poly) {
  // Create array
  std::vector<std::vector<std::array<double, 2>>> polygon;
  // Create polyline
  std::vector<std::array<double, 2>> polyline;
  for (auto& p : poly->points) {
    polyline.push_back({p.x, p.y});
  }
  polygon.push_back(polyline);

  auto indices = mapbox::earcut<uint32_t>(polygon);
  return indices;
}

}  // namespace qlib::tools